package itsm.isperp.module.controller.kpi;

import itsm.isperp.framework.collection.tree.ITreeNode;
import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.domain.PageResponse;
import itsm.isperp.framework.web.domain.CountQuery;
import itsm.isperp.framework.web.request.DefaultCountQuery;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.kpi.KpiSetting;
import itsm.isperp.module.service.change.ChgChangeService;
import itsm.isperp.module.service.kpi.KpiSettingService;
import itsm.isperp.module.service.project.ProjProjectService;
import itsm.isperp.module.utils.EchartGraphLinkUtils;
import itsm.isperp.report.IDataResolver;
import itsm.isperp.report.data.warehouse.QueryRequest;
import itsm.isperp.report.resolver.DataResolver;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2015-12-8
 * @since 1.0
 * 
 */
@Controller
@RequestMapping("kpi/change")
public class KpiChangeController {

	@Autowired
	protected KpiSettingService kpiSettingService;
	
	@Autowired
	protected ProjProjectService projProjectService;

	protected CountQuery createDataCountQuery(String condition) {

		CountQuery cq = new DefaultCountQuery();

		cq.addCountFilter("所有", true, "  1=1  " + " and "+ condition);

		cq.addCountFilter("VIP", true, "  status<>'已关闭'  AND  are_vip=1 and "
				+ condition);

		cq.addCountFilter("SLA超期", true, " sla_status=1 AND  status<>'已关闭' and "
				+ condition);
		cq.addCountFilter("重大", true,
				" priority_name='一级' AND   status<>'已关闭' and " + condition);

		cq.addCountFilter("不满意", true,
				"  satisfaction IN (2,1)  AND feedback_status IS NULL and "
						+ condition);

		cq.addCountFilter("不满意(反馈中)", true,
				"  satisfaction IN (2,1)  AND feedback_status ='1' and  "
						+ condition);

		cq.addCountFilter(
				"多次催办",
				true,
				" status<>'已关闭' AND EXISTS (SELECT source_id FROM app_message_success WHERE MESSAGE_LEVEL=1 AND source_id=id)  and "
						+ condition);
		cq.addCountFilter("挂起", true, " a.status IN ('挂起','挂起审核') and " + condition);

		return cq;
	}

	@RequestMapping("dataMenu")
	@ResponseBody
	public ITreeNode[] serviceDeskMenu() {
		return this.createDataCountQuery("").getTreeNodes();

	}

	protected PageResponse<Map<String, Object>> getListDetail(CountQuery cq,
			JqGridRequest request, HttpServletRequest req) {
		String queryPath = req.getParameter("queryPath");
		ChgChangeService chgChangeService = ContextHolder
				.getSpringBean("chgChangeService");
		
		if (cq != null) {
			request.setCountQuery(cq);
			request.setQueryPath(queryPath);

		} 
		return chgChangeService.findAllDetailByRequest(request).toPage();
	}

	@RequestMapping("dataList")
	@ResponseBody
	public PageResponse<Map<String, Object>> dataList(String chartId,
			String dtype, String gt, String stype, JqGridRequest request,
			HttpServletRequest req) throws ParseException {
		
		String fromType=req.getParameter("fromType");
		String condition="";
		if(StringUtils.isNotEmpty(fromType) && fromType.equals("portal"))
		{
			String key=req.getParameter("key");
			String value=req.getParameter("value");
			condition=EchartGraphLinkUtils.getSql(key, value);
			condition=condition.replace("='空'", " is null ");
		}else
		{
			KpiSetting setting = kpiSettingService.findOne(chartId);

			IDataResolver resolver = null;

			resolver = new DataResolver(setting, dtype, gt, stype, false, req);
			QueryRequest qr = resolver.getQueryRequest();
			StringBuilder sb = resolver.getQueryResponse().getWhereSql();
			String gtvalue = req.getParameter("gtvalue");

			if (gtvalue != null) {

				sb.append(" AND " + qr.getGroup() + " ='" + gtvalue + "'");
			}
		    condition=sb.toString().substring(5);
			condition=condition.replace("='空'", " is null ");
		}
		request.setSearchString(condition);
		ChgChangeService chgChangeService = ContextHolder
        .getSpringBean("chgChangeService");
		return chgChangeService.findAllDetailByRequest(request).toPage();
	}

	@RequestMapping(value = { "dataView" })
	public ModelAndView dataView(String url, HttpServletRequest req)
			throws UnsupportedEncodingException {
		ModelAndView mv = new ModelAndView("kpi/change/dataView");
		mv.addObject("url", URLDecoder.decode(url, "UTF-8"));
		String fromType=req.getParameter("fromType");
		
		mv.addObject(
        "projects",
        projProjectService.findAll(
            new JqGridRequest().isPageable(false).setSelectColumns(
                "id", "name")).getContent());
		if(StringUtils.isNotEmpty(fromType))
		{
			String key=req.getParameter("key");
			String ename=req.getParameter("ename");
			mv.addObject("fromType",fromType);
			mv.addObject("key",key);
			mv.addObject("value",ename);
		}
		return mv;
	}
}
